package freespiritsocial.io.user.ui.fragment.search

import android.os.Bundle
import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import com.scwang.smart.refresh.layout.util.SmartUtil.dp2px
import freespiritsocial.io.user.R
import freespiritsocial.io.user.base.BaseFragment
import freespiritsocial.io.user.data.model.bean.Tag
import freespiritsocial.io.user.databinding.FrgSearchBinding
import freespiritsocial.io.user.decoration.SpaceItemDecoration
import freespiritsocial.io.user.ext.hideSoftKeyboard
import freespiritsocial.io.user.ext.init
import freespiritsocial.io.user.ext.setOnSearchBtnClick
import freespiritsocial.io.user.ui.adapter.search.SearchHistoryAdapter
import freespiritsocial.io.user.ui.fragment.post.PostDetailFrg.Companion.KEY_DATA
import freespiritsocial.io.user.viewmodel.SearchViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction


/**
 * @description 搜索页
 */
class SearchFrg: BaseFragment<SearchViewModel,FrgSearchBinding>() {
    private val forYouFrg by lazy(LazyThreadSafetyMode.NONE) {
        ForYouFrg.crate(this)
    }
    private val searchPeopleFrg by lazy(LazyThreadSafetyMode.NONE) {
        SearchPeopleFrg.crate(this)
    }
    private val suggestionsFrg by lazy(LazyThreadSafetyMode.NONE) {
        SuggestionsFrg.crate(this)
    }
    private val tagsFrg by lazy(LazyThreadSafetyMode.NONE) {
        TagsFrg.crate(this)
    }
    private val adapter by lazy(LazyThreadSafetyMode.NONE) {
        SearchHistoryAdapter().apply {
            headerWithEmptyEnable = true
            addChildClickViewIds(R.id.tv_search_name,R.id.iv_remove)
            setOnItemChildClickListener { adapter,view,position ->
                when (view.id) {
                    R.id.tv_search_name -> {
                        mViewModel.searchText.value = (view as TextView).text.toString()
                        actionSearch()
                    }
                    R.id.iv_remove -> {
                        mViewModel.deleteSearchHistory(this.data[position].historyId ?: return@setOnItemChildClickListener)
                        adapter.removeAt(position)
                    }
                }
            }
        }
    }
    private val resultFragments by lazy(LazyThreadSafetyMode.NONE) {
        arrayListOf<Fragment>(forYouFrg,tagsFrg,suggestionsFrg,searchPeopleFrg)
    }

    override fun layoutId(): Int = R.layout.frg_search

    override fun initView(savedInstanceState: Bundle?) = with(mDatabind) {
        model = mViewModel
        click = Click()
        tab.setViewPager(viewpager,arrayOf("For you","Tags","Suggestions","People"),requireActivity(),resultFragments)
        recyclerHistory.init(adapter,emptyImage = R.mipmap.empty_search,emptyText = "No search history.").also {
            adapter.setHeaderView(TextView(context).apply {
                paint.isFakeBoldText = true
                textSize = 13f
                text = "Search history"
            })
        }.addItemDecoration(SpaceItemDecoration(dp2px(20f),dp2px(15f)))
        etSearch.setOnSearchBtnClick {
            actionSearch()
            mViewModel.addSearchHistory(etSearch.text.toString())
        }
    }

    private fun actionSearch() {
        hideSoftKeyboard(requireActivity())
        mViewModel.searchIng.value = true
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.page.observe(this) {
            mDatabind.tab.setCurrentTab(it,true)
        }
        mViewModel.historyResult.observe(this) {
            adapter.setList(it)
        }
        mViewModel.postResult.observe(this) {
            nav().navigateAction(R.id.postDetailFrg,bundle = bundleOf(KEY_DATA to it))
        }
    }

    override fun lazyLoadData() {
        mViewModel.getSearchHistoryList()
    }

    fun searchTag(tag: Tag) {
        (resultFragments[1] as TagsFrg).findTag(tag)
    }

    inner class Click {
        fun removeSearch() {
            mViewModel.clear()
        }
    }

}